package com.jprocms.module.cms.controller.admin.content;

import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.common.pojo.PageResult;
import com.jprocms.module.cms.controller.admin.content.tagvo.TagCreateReqVO;
import com.jprocms.module.cms.controller.admin.content.tagvo.TagPageReqVO;
import com.jprocms.module.cms.controller.admin.content.tagvo.TagRespVO;
import com.jprocms.module.cms.controller.admin.content.tagvo.TagUpdateReqVO;
import com.jprocms.module.cms.convert.content.TagConvert;
import com.jprocms.module.cms.dal.dataobject.content.TagDO;
import com.jprocms.module.cms.service.content.TagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static com.jpro.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - tag词")
@RestController
@RequestMapping("/cms/tag")
@Validated
public class TagController {

    @Resource
    private TagService tagService;

    @PostMapping("/create")
    @Operation(summary = "创建tag词")
    @PreAuthorize("@ss.hasPermission('cms:tag:create')")
    public CommonResult<Long> createTag(@Valid @RequestBody TagCreateReqVO createReqVO) {
        return success(tagService.createTag(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新tag词")
    @PreAuthorize("@ss.hasPermission('cms:tag:update')")
    public CommonResult<Boolean> updateTag(@Valid @RequestBody TagUpdateReqVO updateReqVO) {
        tagService.updateTag(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除tag词")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:tag:delete')")
    public CommonResult<Boolean> deleteTag(@RequestParam("id") Long id) {
        tagService.deleteTag(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得tag词")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:tag:query')")
    public CommonResult<TagRespVO> getTag(@RequestParam("id") Long id) {
        TagDO tag = tagService.getTag(id);
        return success(TagConvert.INSTANCE.convert(tag));
    }

    @GetMapping("/list")
    @Operation(summary = "获得tag词列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('cms:tag:query')")
    public CommonResult<List<TagRespVO>> getTagList(@RequestParam("ids") Collection<Long> ids) {
        List<TagDO> list = tagService.getTagList(ids);
        return success(TagConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得tag词分页")
    @PreAuthorize("@ss.hasPermission('cms:tag:query')")
    public CommonResult<PageResult<TagRespVO>> getTagPage(@Valid TagPageReqVO pageVO) {
        PageResult<TagDO> pageResult = tagService.getTagPage(pageVO);
        return success(TagConvert.INSTANCE.convertPage(pageResult));
    }
}
